package org.oa.ai.feign;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.oa.ai.chatModel.CcCtCwyjyy;
import org.oa.ai.chatModel.OactbdCsqjsq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

@Service
public class AIFeignService {

    @Autowired
    private AIFeignClient feignClient;

    /**
     * 获取流程节点信息
     * @param nodeId
     * @param processDefinitionKey
     * @return
     */
    public JSONArray nextTasksByNodeId(
                                String nodeId,
                                String processDefinitionKey
    ) {
        String map = feignClient.nextTasksByNodeId(nodeId, processDefinitionKey);
        JSONObject json = JSONUtil.parseObj(map);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray nodeList = content.getJSONArray("data");
            return nodeList;
        }
        return null;
    }

    /**
     * 获取流程节点信息长城
     * @param nodeId
     * @param processDefinitionKey
     * @return
     */
    public JSONArray nextTasksByNodeIdCC(
            String nodeId,
            String processDefinitionKey,
            String userId
    ) {
        String map = feignClient.nextTasksByNodeIdCC(nodeId, processDefinitionKey, userId);
        JSONObject json = JSONUtil.parseObj(map);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray nodeList = content.getJSONArray("data");
            return nodeList;
        }
        return null;
    }

    @RequestMapping(
            value = {"/getFlowUserByNodeId"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    public JSONArray getFlowUserByNodeId(@RequestParam(required = false) String nodeId,
                                      @RequestParam(required = false) String processDefinitionKey,
                                      @RequestParam(required = false) String startUserId,
                                      @RequestParam(required = false) String assigneeId
    ){
        String flowUserByNodeId = feignClient.getFlowUserByNodeId(nodeId, processDefinitionKey, startUserId, assigneeId);
        JSONObject json = JSONUtil.parseObj(flowUserByNodeId);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray userList = content.getJSONArray("nodeDefinationUsers");
            return userList;
        }
        return null;
    }

    @RequestMapping(
            value = {"/getFlowUserByNodeIdCC"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    public JSONArray getFlowUserByNodeIdCC(@RequestParam(required = false) String nodeId,
                                         @RequestParam(required = false) String processDefinitionKey,
                                         @RequestParam(required = false) String startUserId,
                                         @RequestParam(required = false) String assigneeId,
                                         @RequestParam(required = false) String userId
    ){
        String flowUserByNodeId = feignClient.getFlowUserByNodeIdCC(nodeId, processDefinitionKey, startUserId, assigneeId, userId);
        JSONObject json = JSONUtil.parseObj(flowUserByNodeId);
        JSONObject content = json.getJSONObject("content");
        if (content != null) {
            JSONArray userList = content.getJSONArray("nodeDefinationUsers");
            return userList;
        }
        return null;
    }

    @RequestMapping(
            value = {"/startProcessByKeyNew"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    public Map<String, Object> startProcessByKeyNew(@RequestParam(required = true) String businessKey,
                                                    @RequestParam(required = false) String processDefinitionKey,
                                                    @RequestParam(required = false) String title,
                                                    @RequestParam(required = false) String moduleId,
                                                    @RequestParam(required = false) String firstNode,
                                                    @RequestParam(required = false) String reportStatus,
                                                    @RequestParam(required = false) String mijiid,
                                                    @RequestParam(required = false) String tableName,
                                                    @RequestParam(required = false) String comminfoId,
                                                    @RequestParam(required = false) String userId,
                                                    @RequestParam(required = false) String formType) {
        return feignClient.startProcessByKeyNew(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, userId, formType);
    }

    @RequestMapping(
            value = {"/startProcessByKeyNewCC"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    public Map<String, Object> startProcessByKeyNewCC(@RequestParam(required = true) String businessKey,
                                                    @RequestParam(required = false) String processDefinitionKey,
                                                    @RequestParam(required = false) String title,
                                                    @RequestParam(required = false) String moduleId,
                                                    @RequestParam(required = false) String firstNode,
                                                    @RequestParam(required = false) String reportStatus,
                                                    @RequestParam(required = false) String mijiid,
                                                    @RequestParam(required = false) String tableName,
                                                    @RequestParam(required = false) String comminfoId,
                                                    @RequestParam(required = false) String userId,
                                                    @RequestParam(required = false) String formType) {
        return feignClient.startProcessByKeyNewCC(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, userId, formType);
    }

    @PostMapping("/updateReportStatus")
    public Boolean updateReportStatus(@RequestParam(required = true) String taskId,
                                      @RequestParam(required = true) String reportStatus){
        return feignClient.updateReportStatus(taskId,reportStatus);
    }

    /**
     * 流程提交接口
     */
    @RequestMapping(
            value = {"/taskCompleteNew"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    public Map<String, Object> taskCompleteNew(String businessKey,
                                               String currentTaskId,
                                               String nextTaskActivityId,
                                               String nextTaskUserIds,
                                               String nextTaskUsernames,
                                               String assigneeId,
                                               String moduleId,
                                               String nodeName,
                                               String reportStatus,
                                               String nodeCode,
                                               String rwlxs,
                                               String exField5,
                                               String processDefinitionId, // 流程监控传入piid
                                               String currentNodeId, // 流程监控传入的 nodeid
                                               String tjlx,   // 提交类型，判断不为空并且等于 lcjk 说明是流程监控提交的
                                               String logtype,//jump流程跳转
                                               String tableName,
                                               String comminfoId,
                                               String processDefinitionKey,
                                               String formType,
                                               String userId
    ){
        Map<String, Object> map = feignClient.taskCompleteNew(
                businessKey,
                currentTaskId,
                nextTaskActivityId,
                nextTaskUserIds,
                nextTaskUsernames,
                assigneeId,
                moduleId,
                nodeName,
                reportStatus,
                nodeCode,
                rwlxs,
                exField5,
                processDefinitionId,
                currentNodeId,
                tjlx,
                logtype,
                tableName,
                comminfoId,
                processDefinitionKey,
                formType,
                userId
        );
        return map;
    }

    @PostMapping("/ai/flow/addForm")
    public OactbdCsqjsq add(@RequestBody OactbdCsqjsq oactbdCsqjsq){
        OactbdCsqjsq add = feignClient.add(oactbdCsqjsq);
        return add;
    }

    @GetMapping("/ai/flow/getDataById/{id}")
    public OactbdCsqjsq getDataById(@PathVariable("id") String id){
        OactbdCsqjsq result = feignClient.getDataById(id);
        return result;
    }

    @GetMapping(value = "/ai/flow/getFileContent/{id}")
    public String getFileContent(@PathVariable("id") String id){
        return feignClient.getFileContent(id);
    }

    @GetMapping(value = "/ai/flow/getFileByGwid/{id}")
    public File getFileByGwid(@PathVariable("id") String id){
        return feignClient.getFileByGwid(id);
    }

    @PostMapping("/ai/flow/cc/addForm")
    public Map<String, Object> addForm(@RequestBody Map<String, Object> map){
        Map<String, Object> add = feignClient.addForm(map);
        return add;
    }

    @GetMapping("/ai/flow/cc/getDataById/{tableName}/{id}")
    public Map<String, Object> getDataByIdByCC(@PathVariable("tableName") String tableName, @PathVariable("id") String id){
        Map<String, Object> result = feignClient.getDataByIdByCC(tableName, id);
        return result;
    }

    @GetMapping("/ai/flow/cc/getUserByDeptId/{id}")
    public JSONArray getYcrList(@PathVariable("id") String loginDeptId) {
        return feignClient.getYcrList(loginDeptId);
    }

    @PostMapping(value = "/ai/flow/common/fileUpload/{gwid}/{ywlx}/{userId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public JSONObject fileUpload(@RequestPart("file") MultipartFile file, @PathVariable("gwid") String gwid, @PathVariable("ywlx") String ywlx, @PathVariable("userId") String userId){
        return feignClient.fileUpload(file, gwid, ywlx, userId);
    }

    @GetMapping("/ai/flow/getAllUsers/{id}")
    public JSONArray getAllUsers(@PathVariable("id") String userId) {
        return feignClient.getAllUsers(userId);
    }
}